﻿Imports System.Data.SqlClient

Public Class cCompany

    Dim con As New SqlConnection(strcon)

    Public Function select_Company_byID(ByVal CompanyID As Long) As DataTable
        Dim dt As New DataTable
        Dim oMain As New cMain
        dt = oMain.getData("SELECT * FROM tblCompany WHERE CompanyID=N'" & CompanyID & "'")
        Return dt
    End Function

    Public Function insert_Company(ByVal CompanyName As String, ByVal Address As String, ByVal Photo As Byte(), ByVal ContactName As String, ByVal Tel As String, ByVal Email As String, ByVal ContractID As Long, ByVal UserID As String) As Boolean
        Dim rv As Boolean = True
        Dim cmd As New SqlCommand
        cmd.CommandText = "INSERT INTO tblCompany(CompanyName,[Address],Photo,ContactName,Tel,Email,ContractID,UserID) VALUES(@CompanyName,@Address,@Photo,@ContactName,@Tel,@Email,@ContractID,@UserID)"
        With cmd.Parameters
            .AddWithValue("@CompanyName", CompanyName)
            .AddWithValue("@Address", Address)
            If Photo IsNot Nothing Then
                .AddWithValue("@Photo", Photo)
            Else
                .Add("@Photo", SqlDbType.Image).Value = DBNull.Value
            End If
            .AddWithValue("@ContactName", ContactName)
            .AddWithValue("@Tel", Tel)
            .AddWithValue("@Email", Email)
            .AddWithValue("@ContractID", ContractID)
            .AddWithValue("@UserID", UserID)
        End With
        cmd.Connection = con
        Try
            con.Open()
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            rv = False
        Finally
            con.Close()
            cmd.Dispose()
        End Try
        Return rv
    End Function

    Public Function delelte_Company(ByVal ContractID As Long) As Boolean
        Dim rv As Boolean = True
        Dim cmd As New SqlCommand
        cmd.CommandText = "DELETE FROM tblCompany WHERE ContractID=@ContractID"
        With cmd.Parameters
            .AddWithValue("@ContractID", ContractID)
        End With
        cmd.Connection = con
        Try
            con.Open()
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            rv = False
        Finally
            con.Close()
            cmd.Dispose()
        End Try
        Return rv
    End Function

End Class
